构建基于MongoDB的 graphql 服务器(三)

graphql-request的使用

graphql是graphcool的的client端,可以用于express服务器中请求数据,在resolver中也可以使用。apollo-client就只能用在在前端。 对于前端使用apollo-client绝对是最好的选择。目前没有对手。

做mutate处理,遇到不少问题,主要函数函数第一次接触,参数没有拼接对。一直以为是js对象转json的问题,早上一直在解决这个问题。结果还是参数的问题,模拟的数据是hotel的数据,其中嵌套有geolocation信息。 为这个嵌套,改了不同的组合,多没有出来, 结果正确拼接好以后,所有问题都没有了,数据是js对象还是json数据都可处理。

接下来要遍历对象数组的时候问题依然是变量的拼接。

代码如下,直接启动文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//从hotel 文件获取json数据然后写入到graphcool数据库

import express from 'express'
import bodyParser from 'body-parser'
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'
import {makeExecutableSchema} from 'graphql-tools'
import cors from 'cors'
import * as R from 'ramda'
var fs= require('fs');
var path=require('path')
require('es6-promise').polyfill();
require('isomorphic-fetch');
import fetch from 'node-fetch';
import { request } from 'graphql-request'
const URL = 'http://localhost'
const PORT = 3001
export const start = async () => {
try {

const typeDefs = [`
type geoData{
longitude: Float!,
latitude: Float!,
}
type Hotel{
id: ID!
ratingStars: Int!
name: String!
streetAddress: String!
postalCode: String!,
cityLocalized: String!,
geolocation:[geoData!]!
}

type Mutation {
insertData(
name: String
): Hotel
}

type Query{
getHotels: [Hotel]
geoDatas: geoData

}


`];

const resolvers ={
Mutation: {
insertData:async (parent,args,context)=>{
const query = `{
allHotels(name:$name,
streetAddress: $streetAddress,
postalCode: $postalCode,
cityLocalized: $cityLocalized,
geolocation:{$longitude:longitude,$latitude:latitude}){
name,
geolocation{
longitude,
latitude
}
}
}`;
const variables={
ratingStars: 8,
name: "vvafo",
streetAddress: "Hruggerstrasse 56",
postalCode: "8400",
cityLocalized: "Paden",
geolocation: {latitude: 47.343960, longitude: 7.304224}
};
request('https://api.graph.cool/simple/v1/cjaxudkum2ugf0127kok921bc', query,variables).then(data =>
{
console.log(data);
return data

})


}

},

Query:{
getHotels:async (parent,args,context)=>{

const query = `{
allHotels{
id,
name,
geolocation{
longitude,
latitude
}
}
}`

request('https://api.graph.cool/simple/v1/cjaxudkum2ugf0127kok921bc', query).then(data =>
{
console.log(data);
return data

})

},

}

}

const schema = makeExecutableSchema({
typeDefs,
resolvers
})

const app = express()

app.use(cors())
app.use(express.static(__dirname))

app.use('/graphql', bodyParser.json(), graphqlExpress({schema: schema}))

const homePath = '/graphiql'

app.use(homePath, graphiqlExpress({
endpointURL: '/graphql'
}))

app.listen(PORT, () => {
console.log(`Visit ${URL}:${PORT}${homePath}`)
})

InsertData();

} catch (e) {
console.log(e)
}

}

//使用的是这个函数
var InsertData=()=>{
const mu= `mutation createOneHotel(
$name:String!,
$ratingStars:Int!,
$streetAddress:String!,
$postalCode: String!,
$cityLocalized:String!,
$longitude: Float!,
$latitude: Float!

){
createHotel(
name:$name,
ratingStars:$ratingStars,
streetAddress:$streetAddress,
postalCode:$postalCode,
cityLocalized:$cityLocalized,
geolocation:{latitude:$latitude,
longitude: $longitude}){
id
geolocation{
longitude
latitude
}
}
}`;
const Variables={
name:"sdfsdlfrererjsldjfsl",
ratingStars:11,
streetAddress:"fdgtetwerwgeg",
postalCode:"Hakerer ddome",
cityLocalized:"Lordan",
latitude: 47.343964,
longitude: 7.30424,
};
request('https://api.graph.cool/simple/v1/cjaxudkum2ugf0127kok921bc', mu,Variables).then(data =>
{
console.log(data);
return data

})

};